package com.gyms.manager;

import android.content.Context;
import android.net.http.SslError;
import android.os.Handler;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.classic.okhttp.beans.HVRefundConfigBean;
import com.google.gson.Gson;
import com.gyms.bean.RevertRuleBean;

import java.util.ArrayList;
import java.util.List;

import utils.StringUtils;

/**
 * Webview的相关设置
 * 林天训
 */
public class WebViewManger {
    private static WebViewManger sWebViewManger;
    private Context mContext;

    private WebViewManger(Context mContext) {
        this.mContext = mContext.getApplicationContext();
    }
    private Handler mHandler=new Handler();

    public static WebViewManger getInstance(Context mContext) {
        if (null == sWebViewManger) {
            sWebViewManger = new WebViewManger(mContext);
        }
        return sWebViewManger;
    }

    /**
     * webView 相关设置 以及cookie同步
     * @param mWebView     webview
     * @param url          链接
     * @param mProgressBar 进度条
     * @param isRevertDetail 是否是退款详情
     * @param refundConfigs 退款规则
     * @param venueItemSpecific html字符串
     * @param mTitle 网页的title
     * @param mTvTitle 标题VIew
     * @param mTvAfterBack 紧贴返回按钮的后退键
     */
    public void setWebViewSetting(final WebView mWebView, String url, final ProgressBar mProgressBar, final boolean isRevertDetail, final ArrayList<HVRefundConfigBean> refundConfigs, String venueItemSpecific, final String mTitle, final TextView mTvTitle, final TextView mTvAfterBack) {
         /*webview加载进度条*/
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);  //设置 缓存模式

        mWebView.getSettings().setDomStorageEnabled(true);//支持domstorage 若不加会加载不出某些html
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    mProgressBar.setVisibility(View.GONE);
                } else {
                    mProgressBar.setProgress(newProgress);
                    if (mProgressBar.getVisibility() == View.GONE) {
                        mProgressBar.setVisibility(View.VISIBLE);
                    }
                }
                super.onProgressChanged(view, newProgress);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if(StringUtils.isNull(mTitle)){//如果没传title
                    mTvTitle.setText(title);
                }

            }
        });

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                    mTvAfterBack.setVisibility(mWebView.canGoBack()?View.VISIBLE:View.GONE);
                if (isRevertDetail) {// 退款规则 加载js方法啊
                    List<RevertRuleBean> mRevertBeans=new ArrayList<>();
                    for(HVRefundConfigBean configBean:refundConfigs){
                        RevertRuleBean mBean=new RevertRuleBean();
                        String mStrTime=OrderManger.getTimeFromS(configBean.getDueTime());
                        mBean.setRatio(configBean.getRatio()+"%");
                        mBean.setTime(mStrTime);
                        mRevertBeans.add(mBean);
                    }
                    Gson mGson = new Gson();
                    final String mStr = mGson.toJson(mRevertBeans);//退款规则
                    mWebView.loadUrl("javascript:moneyBack(" + mStr + ")");
                }

            }
        });

        mTvAfterBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWebView.goBack();
            }
        });
            mWebView.addJavascriptInterface(new AndroidJavaScript(mContext), "HvJsApi");
            if(StringUtils.isNull(url)&&!StringUtils.isNull(venueItemSpecific)){
                mWebView.loadDataWithBaseURL(null, venueItemSpecific, "text/html", "utf-8",
                        null);

            }else
            mWebView.loadUrl(url);
    }
}
